﻿using System;
using System.Collections.Generic;
using System.IO;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	internal class DimInHomePipe
	{
		public DimInHomePipe(ExternalCommandData comman, FormInHomePipe form, bool bis)
		{
			this.commandData = comman;
			this.formDim = form;
			this.doc = this.commandData.Application.ActiveUIDocument.Document;
			this.IsPlan = bis;
		}

		public bool Run()
		{
			using (DimInHomeJig dimInHomeJig = new DimInHomeJig(this.commandData.Application.ActiveUIDocument, this.formDim.fromData))
			{
				if (!dimInHomeJig.startPoint() || !dimInHomeJig.endPoint())
				{
					return false;
				}
				this.LayDim(dimInHomeJig.refPipe, dimInHomeJig.PointEnd);
			}
			return true;
		}

		private bool LayDim(Reference refPipe, XYZ dimPoint)
		{
			string fsName;
			if (this.formDim.fromData.DimStype == 1)
			{
				fsName = "编号在下";
			}
			else
			{
				fsName = "编号在上";
			}
			Pipe pipe = this.doc.GetElement(refPipe.ElementId) as Pipe;
			if (pipe == null)
			{
				return false;
			}
			Transaction transaction = null;
			try
			{
				transaction = new Transaction(this.doc, "标注入户管号");
				transaction.Start();
				bool isCustomNumber = this.formDim.fromData.DimContent == 2;
				DimInHomePipe.SetInHomePipeParameters(this.commandData.Application, pipe, this.formDim.fromData.Content, isCustomNumber);
				string text = "";
				FamilySymbol markTagSymbolByName = YJKRevitTools.GetMarkTagSymbolByName("MarkTag", "入户管号标注", fsName, this.doc, ref text, BuiltInCategory.OST_PipeTags);
				if (markTagSymbolByName == null)
				{
					transaction.RollBack();
					return false;
				}
				double num = dimPoint.Z;
				if (this.IsPlan)
				{
					num = this.doc.ActiveView.GenLevel.Elevation;
				}
				XYZ xyz = new XYZ(dimPoint.X, dimPoint.Y, num);
				IndependentTag independentTag = RevitVersionFuncs.NewTag(this.doc, this.doc.ActiveView, new Reference(pipe), this.formDim.fromData.IsLead, 0, 0, xyz);
				independentTag.ChangeTypeId(markTagSymbolByName.Id);
				independentTag.TagHeadPosition = xyz;
				AssociationMarkInfo.WriteMarkInfoToElement(independentTag, MarkType.PipeLine);
				transaction.Commit();
			}
			catch
			{
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return true;
		}

		public static List<PipingSystemType> GetAllPipeSystemType(Autodesk.Revit.DB.Document doc)
		{
			List<PipingSystemType> list = new List<PipingSystemType>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(PipingSystemType)).ToElements())
			{
				PipingSystemType item = element as PipingSystemType;
				list.Add(item);
			}
			return list;
		}

		public static bool SetInHomePipeParameters(UIApplication uiApp, Pipe pipe, string number, bool isCustomNumber)
		{
			bool result = false;
			try
			{
				string shareFilePath = Path.Combine(Product.DataLocation, "HYShared Parameters.txt");
				DefinitionFile defineFile = YJKRevitTools.OpenShareParamFile(uiApp, shareFilePath);
				CategorySet bdcategorySet = YJKRevitTools.GetBDCategorySet(uiApp, BuiltInCategory.OST_PipingSystem);
				TypeBinding elemBinding = YJKRevitTools.CreatElemBinding(uiApp, bdcategorySet, "type") as TypeBinding;
				YJKRevitTools.SetShareParameter(uiApp, defineFile, "管道参数", new List<string>
				{
					"入户管号"
				}, elemBinding, (BuiltInParameterGroup)(-5000123));
				List<PipingSystemType> allPipeSystemType = DimInHomePipe.GetAllPipeSystemType(uiApp.ActiveUIDocument.Document);
				if (allPipeSystemType != null && allPipeSystemType.Count > 0 && pipe != null)
				{
					Parameter parameter = pipe.get_Parameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM);
					if (parameter != null)
					{
						foreach (PipingSystemType pipingSystemType in allPipeSystemType)
						{
							if (parameter.AsElementId() == pipingSystemType.Id)
							{
								int num = 0;
								Parameter parameter2 = pipingSystemType.GetParameter("入户管号");
								if (parameter2 != null)
								{
									num = Convert.ToInt32(parameter2.AsString());
								}
								Parameter parameter3 = pipe.GetParameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
								if (parameter3 != null)
								{
									if (isCustomNumber)
									{
										parameter3.Set(number);
										int num2 = 1;
										if (int.TryParse(number, out num2) && num2 > num)
										{
											num = Convert.ToInt32(number) + 1;
										}
									}
									else
									{
										num++;
										parameter3.Set(num.ToString());
									}
								}
								if (parameter2 != null)
								{
									parameter2.Set(num.ToString());
									break;
								}
								break;
							}
						}
						result = true;
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return result;
		}

		private ExternalCommandData commandData;

		private FormInHomePipe formDim;

		private Document doc;

		private bool IsPlan = true;
	}
}
